<template>
	<view :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('user.order.myorder')}}
				</view>
				<view class="action">
					<!-- <text class="cuIcon-search"></text> -->
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="text-tips margin-top text-center text-gray text-sm">
			{{$t('user.order.dislastrecord')}}
		</view>
		<view class="no-data" v-if="orderList.length==0">
			<text class="cuIcon-text"></text>{{$t('common.nodata')}}
		</view>
		<view class="order-box" v-for="(item,index) in orderList" :key="index">
			<view class="order-main">
				<view class="order-info">
					<text class="order-id">{{$t('user.order.orderid')}}:{{item.id}} {{item.created}}</text>
					<text class="order-type bg-blue" v-if="item.shopInfo">{{getType(item.shopInfo.type)}}</text>
				</view>
				<view class="order-shop" @tap="shopInfo(item.shopInfo.id)" v-if="item.shopInfo.type!=4">
					{{item.shopInfo.title}}
					<view class="order-time">{{formatDate(item.created)}}</view>
				</view>
				<view class="order-shop" @tap="toInfo(item.shopInfo)" v-else>
					{{$t('user.order.paycontent')}}
					<view class="order-time">{{formatDate(item.created)}}</view>
				</view>
				<view class="order-shop" v-if="!item.shopInfo">
					{{$t('user.order.productexpir')}}
					<view class="order-time">{{formatDate(item.created)}}</view>
				</view>
				<view class="order-btn" v-if="item.shopInfo">
					<text class="text-red">{{item.shopInfo.price + currencyName}}</text>
					<text class="text-green margin-left" @tap="toMerchant(item.shopInfo.uid)">{{$t('user.order.contactseller')}}</text>
					<text class="text-blue" @tap="toInfo(item.shopInfo)">{{$t('user.order.viewpurchasetxt')}}</text>
				</view>
			</view>
		</view>

		<!--加载遮罩-->
		<view class="loading" v-if="isLoading==0">
			<view class="loading-main">
				<image src="../../static/loading.gif"></image>
			</view>
		</view>
		<!--加载遮罩结束-->
	</view>
</template>

<script>
	import {
		localStorage
	} from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar: this.StatusBar + this.CustomBar,
				AppStyle: this.$store.state.AppStyle,
				isLoad: 0,
				token: "",
				orderList: [],
				isLoading: 0,
				currencyName: ""
			}
		},
		onPullDownRefresh() {
			var that = this;
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onShow() {
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if (localStorage.getItem('token')) {
				that.token = localStorage.getItem('token');
			}
			that.getOrderList();
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
		},

		mounted() {
			this.getset();
		},
		methods: {
			getset() {
				var that = this;
				uni.request({
					url: that.$API.SPset(),
					method: 'GET',
					dataType: "json",
					success(res) {
						that.currencyName = res.data.assetsname;
					},
					fail(error) {
						
					}

				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			getType(i) {
				var arr = [this.$t('global.hardgood'),, this.$t('global.softres'), this.$t('global.payread')];
				return arr[i - 1];
			},
			getOrderList() {
				var that = this;
				var data = {
					"token": that.token
				}
				that.$Net.request({
					url: that.$API.orderList(),
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						if (res.data.code == 1) {
							var list = res.data.data;
							if (list.length > 0) {
								that.orderList = list;
							}
						}
						var timer = setTimeout(function() {
							that.isLoading = 1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
						var timer = setTimeout(function() {
							that.isLoading = 1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			// #ifdef H5 || APP-PLUS
			shopInfo(sid) {
				uni.navigateTo({
					url: '/pages/shop/shopinfo?sid=' + sid
				});
			},
			// #endif 
			toInfo(data) {
				uni.navigateTo({
					url: '/pages/shop/shoptext?sid=' + data.id
				});
			},
			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				//second = ("0" + date.getSeconds()).slice(-2);
				// 拼接
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				// 返回
				return result;
			},
			toMerchant(uid) {
				var that = this;
				var token = "";
				var myUid = 0;
				if (localStorage.getItem('userinfo')) {
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token = userInfo.token;
					myUid = userInfo.uid;
				} else {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					uni.navigateTo({
						url: '/pages/user/login'
					});
					return false;
				}

				var touid = uid;
				if (myUid == touid) {
					uni.showToast({
						title: that.$t('user.order.nooneselfchat'),
						icon: 'none'
					})
					return false;
				}

				var data = {
					"touid": touid,
					"token": token
				}
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				that.$Net.request({
					url: that.$API.getPrivateChat(),
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function() {
							uni.hideLoading();
						}, 1000);

						if (res.data.code == 1) {
							var chatid = res.data.data
							// 获取卖家信息并跳转到聊天页面
							that.getUserInfoAndChat(touid, chatid);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail: function(res) {
						setTimeout(function() {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			getUserInfoAndChat(uid, chatid) {
				var that = this;
				var token = "";
				if (localStorage.getItem('userinfo')) {
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token = userInfo.token;
				}
				uni.showLoading({
					title: that.$t('user.order.getsellerinfo')
				});
				that.$Net.request({
					url: that.$API.getUserInfo(),
					data: {
						"key": uid,
						"token": token
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function() {
							uni.hideLoading();
						}, 500);
						if (res.data.code == 1) {
							var data = res.data.data;
							var name = data.name;
							if (data.screenName && data.screenName !== "" && data.screenName !== null) {
								name = data.screenName;
							}
							// 跳转到聊天页面
							uni.navigateTo({
								url: '/pages/chat/chat?uid=' + uid + "&name=" + name + "&chatid=" + chatid
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail: function(res) {
						setTimeout(function() {
							uni.hideLoading();
						}, 500);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			ToCopy(text) {
				var that = this;
				// #ifdef APP-PLUS
				uni.setClipboardData({
					data: text,
					success: () => {
						uni.showToast({ //提示
							title: that.$t('common.copysuccess')
						})
					}
				});
				// #endif
				// #ifdef H5 
				uni.showToast({ //提示
					title: that.$t('common.copysuccess')
				})
				let textarea = document.createElement("textarea");
				textarea.value = text;
				textarea.readOnly = "readOnly";
				document.body.appendChild(textarea);
				textarea.select();
				textarea.setSelectionRange(0, text.length);

				var result = document.execCommand("copy")
				textarea.remove();
				// #endif
			},
		}
	}
</script>